<!DOCTYPE html>
<%@ page import="rentadomus.Place" %>  
<%@ page import="rentadomus.Warehouse" %>  
<%@ page import="rentadomus.Office" %>  
<%@ page import="rentadomus.Local" %>  
<%@ page import="rentadomus.Apartament" %>  
<%@ page import="rentadomus.Dwelling" %>  
<%@ page import="rentadomus.House" %>  
<html>
  <head>
    <meta name="layout" content="main"/><!--esta linea es para mostrar el contenido en main-->
    <title>Home - RentADomus</title>
  </head>
  <body  BGCOLOR="red">

  <g:form url="[controller:'searchByString', action:'index', id:'a']">
    <g:textField name="string"/>
    <label for="SearchBy">
      Sort by: 
    </label>
    <g:select name="SearchBy" from="${['Price: Lowest first', 'Price: Highest first','Area: Lowest first', 'Area: Highest first']}" keys="${number}"/>

    <g:submitButton name="searchButton" value="Search" />
  </g:form>

  <div class="message" role="status">${flash.message}</div>
    <g:if test="${!session.person}">
      <div style="width: auto;text-align: right">
        <a href="login/login" class="list">
          <div onmouseover="this.style.backgroundColor='#D8D8D8'" 
               onmouseout="this.style.backgroundColor='#FFFFFF'" class="focusTab">
            Login
          </div>
        </a>
        <a href="person/create" class="list">
          <div  onmouseover="this.style.backgroundColor='#D8D8D8'"
                onmouseout="this.style.backgroundColor='#FFFFFF'" class="focusTab">
            SignUp
          </div>
        </a>
      </div>
    </g:if>
    <g:else>
      <div style="width: auto;text-align: right">
        You are logged in as: <g:link class="list" action="show" controller="person" id="${session.person.id}">${session.person}</g:link>
      </div><br/>
      <g:form name="links">
        <fieldset class="buttons" style="text-align: center">
          <g:link class="list" action="index" controller="placeRedirect">Create new Place</g:link>
          <g:link class="list" action="index" controller="listPlace">List Places</g:link>
          <g:link class="list" action="list" controller="contract">Contracts</g:link>
          <g:link class="list" action="list" controller="pqr">PQR</g:link>
          <g:link class="list" action="logout" controller="login">Log out</g:link>
        </fieldset>
      </g:form>
    </g:else>
    <br>
    <hr width=100% size ="10" align="right" color="#300000" />
    <br>
    <h3 style="text-align: center">
      <g:form name ="Bpl" url="[controller:'searchByType', action:'index']">
        <g:submitButton name="Buscar" value="Search places"/>
      </g:form>
    </h3>
    <h1 align="left"> Recent added places</h1>
    <br>
    <table>
    <tr>
    <g:each in="${Place.findAll(sort: 'date', order:'desc')}" status="i" var="it">
      <g:if test="${((i!=0)&&(i%3==0))}">
        <tr>
      </g:if>
      <g:each in="${Warehouse.findAll()}" status="j" var="ot">          
        <g:if test="${ot.idPlace.id == it.id}">
          <p>
          <td>  
          <g:if test="${it.mainImage != 0}">
            <a href="${createLink(controller:"Warehouse", action:'show', id:ot.id)}"><img class="big" src="${createLink(controller:'UploadImage', action:'displayImage', id:(int)it.mainImage)}" /> </a>
          </g:if>
          <g:else>
            <img src="/RentADomus/images/notav.jpg" />
          </g:else>
          </p>     
          <p><a href="${createLink(controller:"Warehouse", action:'show', id:ot.id)}">${it.titlePlace}</a></p> 
          </td>
        </g:if>
        
      </g:each>        

      <g:each in="${Office.findAll()}" status="j" var="ot">  
        <g:if test="${ot.idPlace.id == it.id}">
          <td>
          <p>
          <g:if test="${it.mainImage != 0}">
            <a href="${createLink(controller:"Office", action:'show', id:ot.id)}"><img class="big" src="${createLink(controller:'UploadImage', action:'displayImage', id:(int)it.mainImage)}" /> </a>
          </g:if>
          <g:else>
            <img src="/RentADomus/images/notav.jpg" />
          </g:else>            
          </p>     
          <p><a href="${createLink(controller:"Office", action:'show', id:ot.id)}">${it.titlePlace}</a></p>                    
        </g:if>
        </td>
      </g:each>        


      <g:each in="${Local.findAll()}" status="j" var="ot">    
        
        <g:if test="${ot.idPlace.id == it.id}">
          <td>
          <p>
          <g:if test="${it.mainImage != 0}">
            <a href="${createLink(controller:"Local", action:'show', id:ot.id)}"><img class="big" src="${createLink(controller:'UploadImage', action:'displayImage', id:(int)it.mainImage)}" /> </a>
          </g:if>
          <g:else>
            <img src="/RentADomus/images/notav.jpg" />
          </g:else>            
          </p>     
          <p><a href="${createLink(controller:"Local", action:'show', id:ot.id)}">${it.titlePlace}</a></p>                    
          </td>
        </g:if>
        
      </g:each>        

      <g:each in="${Dwelling.findAll()}" status="j" var="ot">          
        
        <g:if test="${ot.idPlace.id == it.id}">
          
          <g:each in="${Apartament.findAll()}" status="k" var="at">          
            <g:if test="${ot.id == at.idDwelling.id}">            
              <td>
              <p>
              <g:if test="${it.mainImage != 0}">
                <a href="${createLink(controller:"Apartament", action:'show', id:at.id)}"><img class="big" src="${createLink(controller:'UploadImage', action:'displayImage', id:(int)it.mainImage)}" /> </a>
              </g:if>
              <g:else>
                <img src="/RentADomus/images/notav.jpg" />
              </g:else>                
              </p>     
              <p><a href="${createLink(controller:"Apartament", action:'show', id:at.id)}">${it.titlePlace}</a></p>                    
              </td>
            </g:if>
          </g:each>
        </g:if>
        
      </g:each>          

      <g:each in="${Dwelling.findAll()}" status="j" var="ot">          
        
        <g:if test="${ot.idPlace.id == it.id}">
          
          <g:each in="${House.findAll()}" status="k" var="at">          
            <g:if test="${ot.id == at.idDwelling.id}">           
              <td>
              <p>
              <g:if test="${it.mainImage != 0}">
                <a href="${createLink(controller:"House", action:'show', id:at.id)}"><img class="big" src="${createLink(controller:'UploadImage', action:'displayImage', id:(int)it.mainImage)}" /> </a>
              </g:if>
              <g:else>
                <img src="/RentADomus/images/notav.jpg" />
              </g:else>                
              </p>     
              <p><a href="${createLink(controller:"House", action:'show', id:at.id)}">${it.titlePlace}</a></p>                    
              </td>
              
            </g:if>
          </g:each>
         
        </g:if>
        
      </g:each>          

      <g:if test="${i!=0 &&(i+1)%3==0}">
        </tr>
      </g:if>
    </g:each>    
   </table>
  </body>
</html>



